//
//  DCLoanListController.m
//  Demo
//
//  Created by Kyle on 2022/12/31.
//

#import "DCLoanListController.h"
#import "DCLoanListCell.h"
#import "DCLoanDetialController.h"
#import "DCOrderModel.h"

#define CurrentOrderType  @"1"

#define HistoryOrderType  @"2"

@interface DCLoanListController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UIButton *currentBtn;

@property (nonatomic, strong) UIButton *historyBtn;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataSource;

@property (nonatomic, copy) NSString *orderType;

@property (nonatomic, strong) NSMutableArray *currentOrderArr;

@property (nonatomic, strong) NSMutableArray *historyOrderArr;

@property (nonatomic, strong) DCOrderListModel *currentListModel;

@property (nonatomic, strong) DCOrderListModel *historyListModel;




@end

@implementation DCLoanListController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitleName:@"Loan list"];
    [self getDataFromNet:YES];
    // Do any additional setup after loading the view.
}

- (void)configureAlloc {
    self.dataSource = @[@"",@"",@"",@"",@"",@""].mutableCopy;
    self.currentOrderArr = @[].mutableCopy;
    self.historyOrderArr = @[].mutableCopy;
    _orderType = CurrentOrderType;
}

- (void)getDataFromNet:(BOOL)isRefrash{
    NSInteger pageNum = [self.orderType isEqualToString:CurrentOrderType] ? self.currentListModel.nextPage : self.historyListModel.nextPage;
    [DCHttpRequest DCOrderListType:_orderType pageNum:pageNum complate:^(NSURLSessionDataTask * _Nonnull operation, DCResponseData * _Nonnull responseObject) {
        if (responseObject.code == 200) {
//            NSMutableArray *mArr = @[].mutableCopy;
//            for (NSDictionary *dic in [responseObject.data objectForKey:@"orderRecordList"]) {
//                NSMutableDictionary *mDic = @{}.mutableCopy;
//                for (NSString *key in [dic allKeys]) {
//                    if(![key isEqualToString:@"slogan"]) {
//                        [mDic setObject:dic[key] forKey:key];
//                    }
//                }
//                [mArr addObject:mDic];
//            }
//            DLog(@"%@",mArr.mj_JSONString);
            
            NSArray *dataArr = [DCOrderModel mj_objectArrayWithKeyValuesArray:[responseObject.data objectForKey:@"orderRecordList"]];
            DCOrderListModel *listModel = [DCOrderListModel mj_objectWithKeyValues:responseObject.data];
            DLog(@"%@",dataArr);
            if (isRefrash) {
                [self.orderType isEqualToString:CurrentOrderType]?[self.currentOrderArr removeAllObjects]:[self.historyOrderArr removeAllObjects];
            }
            if ([self.orderType isEqualToString:CurrentOrderType]) {
                [self.currentOrderArr addObjectsFromArray:dataArr];
                self.currentListModel = listModel;
            } else {
                [self.historyOrderArr addObjectsFromArray:dataArr];
                self.historyListModel = listModel;
            }
            [self dealDataForUI];
        } else {
            [self dealDataForUI];
            [DCDCToast toastText:responseObject.message];
        }
    }];
}

- (void)dealDataForUI {
    
    if (self.tableView.mj_header.isRefreshing) {
        [self.tableView.mj_header endRefreshing];
    } else {
//        DCOrderListModel *listModel = [self.orderType isEqualToString:CurrentOrderType] ? self.currentListModel : self.currentListModel;
        [self.tableView.mj_footer endRefreshing];
    }
    NSArray *targetArr = [_orderType isEqualToString:CurrentOrderType] ? self.currentOrderArr : self.historyOrderArr;
    if(!targetArr.count){
        [self.tableView addStatueViewWithDesString:@"暂无数据" statue:DCStatusViewNoData];
    }else {
        [self.tableView removeStatuView];
    }
    
    [self.tableView reloadData];

}

- (void)configureUI {
    [super configureUI];
//    self.navgitBar.hidden = YES;
    self.navgitBar.backBtn.hidden = YES;

    _currentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _currentBtn.layer.cornerRadius = 8;
    _currentBtn.layer.masksToBounds = YES;
    _currentBtn.backgroundColor = [UIColor colorWithString:@"#F0FFCE"];
    [_currentBtn setTitleColor:[UIColor colorWithString:@"#000000"] forState:UIControlStateNormal];
    _currentBtn.titleLabel.font = DCFont_PF_Medium(14);
//    [_currentBtn setImage:[UIImage imageWithColor:[UIColor colorWithString:@"#F7F7F7"]] forState:UIControlStateNormal];
//    [_currentBtn setImage:[UIImage imageWithColor:[UIColor colorWithString:@"#F0FFCE"]] forState:UIControlStateSelected];
    [_currentBtn setTitle:@"Current" forState:UIControlStateNormal];
    [_currentBtn addTarget:self action:@selector(currentAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_currentBtn];
    [_currentBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(32);
        make.top.mas_equalTo(self.navgitBar.mas_bottom).mas_offset(26);
        make.right.mas_equalTo(self.view.mas_centerX).mas_offset(-4);
        make.height.mas_equalTo(40);
    }];
    
    _historyBtn =  [UIButton buttonWithType:UIButtonTypeCustom];
    _historyBtn.layer.masksToBounds = YES;
    _historyBtn.layer.cornerRadius = 8;
    _historyBtn.backgroundColor = [UIColor colorWithString:@"#F7F7F7"];
    [_historyBtn setTitleColor:[UIColor colorWithString:@"#6F6F6F"] forState:UIControlStateNormal];
    _historyBtn.titleLabel.font = DCFont_PF_Regular(14);

//    [_historyBtn setImage:[UIImage imageWithColor:[UIColor colorWithString:@"#F7F7F7"]] forState:UIControlStateNormal];
//    [_historyBtn setImage:[UIImage imageWithColor:[UIColor colorWithString:@"#F0FFCE"]] forState:UIControlStateSelected];
    [_historyBtn setTitle:@"History" forState:UIControlStateNormal];
    [_historyBtn addTarget:self action:@selector(historyAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_historyBtn];
    [_historyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_centerX).mas_offset(4);
        make.top.mas_equalTo(self.navgitBar.mas_bottom).mas_offset(26);
        make.right.mas_equalTo(-32);
        make.height.mas_equalTo(40);
    }];
    
    UIButton *privacyAgreementBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [privacyAgreementBtn setTitle:@"《Privacy Agreement》" forState:UIControlStateNormal];
    [privacyAgreementBtn addTarget:self action:@selector(privacyAgreementBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [privacyAgreementBtn setTitleColor:[UIColor colorWithString:@"#000000"] forState:UIControlStateNormal];
    privacyAgreementBtn.titleLabel.font = DCFont_PF_Regular(11);
    [self.view addSubview:privacyAgreementBtn];
    [privacyAgreementBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.bottom.mas_equalTo(-20);
        make.height.mas_equalTo(16);
    }];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [self.tableView registerClass:DCLoanListCell.class forCellReuseIdentifier:NSStringFromClass(DCLoanListCell.class)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = self.view.backgroundColor ;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_currentBtn.mas_bottom).mas_offset(25);
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(privacyAgreementBtn.mas_top).mas_offset(-10);
    }];
    [self.tableView addNormalHeaderTarget:self action:@selector(tableHeaderRefrash)];
    [self.tableView addNormalFooterTarget:self action:@selector(tableFooterRefrash)];
}

- (void)tableHeaderRefrash{
    [self getDataFromNet:YES];
}
- (void)tableFooterRefrash {
    [self getDataFromNet:NO];
}
#pragma mark --- tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_orderType isEqualToString:CurrentOrderType] ? self.currentOrderArr.count : self.historyOrderArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DCLoanListCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(DCLoanListCell.class)];
    DCOrderModel *model = [_orderType isEqualToString:CurrentOrderType] ? [self.currentOrderArr objectAtIndex:indexPath.row]:[self.historyOrderArr objectAtIndex:indexPath.row];
    [cell configureData:model];
//    NSDictionary *dic = [self.dataSource objectAtIndex:indexPath.row];
//    [cell setDataWithDic:dic lineHidden:indexPath.row == self.dataSource.count-1];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 137;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DCOrderModel *model = [_orderType isEqualToString:CurrentOrderType] ? [self.currentOrderArr objectAtIndex:indexPath.row]:[self.historyOrderArr objectAtIndex:indexPath.row];
    DCLoanDetialController *detailVC = [[DCLoanDetialController alloc] init];
    detailVC.orderModel = model;
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (void)currentAction:(UIButton *)sender {
    _orderType = CurrentOrderType;
    _currentBtn.backgroundColor = [UIColor colorWithString:@"#F0FFCE"];
    [_currentBtn setTitleColor:[UIColor colorWithString:@"#000000"] forState:UIControlStateNormal];
    _currentBtn.titleLabel.font = DCFont_PF_Medium(14);
    
    _historyBtn.backgroundColor = [UIColor colorWithString:@"#F7F7F7"];
    [_historyBtn setTitleColor:[UIColor colorWithString:@"#6F6F6F"] forState:UIControlStateNormal];
    _historyBtn.titleLabel.font = DCFont_PF_Regular(14);
}

- (void)historyAction:(UIButton *)sender {
    _orderType = HistoryOrderType;
    _currentBtn.backgroundColor = [UIColor colorWithString:@"#F7F7F7"];
    [_currentBtn setTitleColor:[UIColor colorWithString:@"#6F6F6F"] forState:UIControlStateNormal];
    _currentBtn.titleLabel.font = DCFont_PF_Regular(14);
    
    _historyBtn.backgroundColor = [UIColor colorWithString:@"#F0FFCE"];
    [_historyBtn setTitleColor:[UIColor colorWithString:@"#000000"] forState:UIControlStateNormal];
    _historyBtn.titleLabel.font = DCFont_PF_Medium(14);
    
}

- (void)privacyAgreementBtnAction:(UIButton *)sender {
    DCWebModel *webModel = [DCWebModel creatLoanAgreement];
    DCWebViewController *webVC = [[DCWebViewController alloc] initWithModel:webModel];
    [self.navigationController pushViewController:webVC animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
